﻿Imports Publics
Imports DanhMuc
Imports System.Windows.Forms


Public Class FrmDMVatTu

    Private _LaChon As Boolean
    Public Property LaChon() As Boolean
        Get
            Return _LaChon
        End Get
        Set(ByVal value As Boolean)
            _LaChon = value
        End Set
    End Property

    Private _LaThoat As String
    Public Property LaThoat() As String
        Get
            Return _LaThoat
        End Get
        Set(ByVal value As String)
            _LaThoat = value
        End Set
    End Property


    Private _IDVatTu As String
    Public Property IDVatTu() As String
        Get
            Return _IDVatTu
        End Get
        Set(ByVal value As String)
            _IDVatTu = value
        End Set
    End Property

    Private _objVatTu As VT_DM_VatTu
    Public Property objVatTu() As VT_DM_VatTu
        Get
            Return _objVatTu
        End Get
        Set(ByVal value As VT_DM_VatTu)
            _objVatTu = value
        End Set
    End Property

    Private _MaVatTu As String
    Public Property MaVatTu() As String
        Get
            Return _MaVatTu
        End Get
        Set(ByVal value As String)
            _MaVatTu = value
        End Set
    End Property
    Public Shared Them, Sua, Xoa, SaoChep As Boolean
    Private Sub PhanQuyen()
        btnThemMoi.Visibility = IIf(Them, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnCopy.Visibility = IIf(SaoChep, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnSuaDoi.Visibility = IIf(Sua, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnXoa.Visibility = IIf(Xoa, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
    End Sub
    Private Sub FrmVatTu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        btnThemMoi.Caption = BtnAddNewCaption
        btnSuaDoi.Caption = BtnEditCaption
        btnXoa.Caption = BtnDeleteCaption
        btnXuatExcel.Caption = BtnExportDataCaption
        btnThoat.Caption = BtnCloseCaption
        btnCopy.Caption = BtnCopyCaption

        If Not LaChon Then
            btnChon.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        End If
        GroupControl1.Text = GetLabelPhimTatCoBan("", True, True, True)

        FillDataToGrid(False)
        SetFilterConditionMa(grvVatTu, grdColMaVatTu, grdColMaVatTu.FieldName, MaVatTu, LaChon)
        PhanQuyen()
    End Sub

    Private Sub FillDataToGrid(Optional ByVal LoadDataFromDB As Boolean = True)
        grdVatTu.DataSource = (From ds In dbTV.VT_DM_VatTus Join dsnhom In dbTV.VT_DM_NhomVatTus On ds.IDNhomVatTu Equals dsnhom.ID _
                               Where ds.InUsed Select New With { _
                               .ID = ds.ID, .MaVatTu = ds.MaVatTu, .TenVatTu = ds.TenVatTu, .DVT = ds.DVT, _
                               .GiaBan = ds.GiaBan.GetValueOrDefault, .MaNhomVatTu = dsnhom.MaNhom, .ThueSuat = ds.ThueSuat.GetValueOrDefault}).ToList
        grdVatTu.RefreshDataSource()
    End Sub

    Private Sub FrmVatTu_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then
            btnThoat_ItemClick(Nothing, Nothing)
        End If
        If e.KeyCode = Keys.Enter And LaChon Then
            IDVatTu = grvVatTu.GetRowCellValue(grvVatTu.FocusedRowHandle, grdColID).ToString
            Me.Close()
        End If
        'Thêm mới
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pADD, btnThemMoi) Then
            btnThemMoi_ItemClick(Nothing, Nothing)
        End If
        'Sửa đổi
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEDIT, btnSuaDoi) Then
            btnSuaDoi_ItemClick(Nothing, Nothing)
        End If
        'Xóa
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pDELETE, btnXoa) Then
            btnXoa_ItemClick(Nothing, Nothing)
        End If
        'Xuất
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEXPORT, btnXuatExcel) Then
            btnXuatExcel_ItemClick(Nothing, Nothing)
        End If
        'Nạp lại
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pREFRESH, btnNapLai) Then
            btnNapLai_ItemClick(Nothing, Nothing)
        End If

    End Sub

    Private Sub btnThemMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThemMoi.ItemClick
        Dim frm As New FrmCapNhatVatTu
        frm.Action = vAddAction
        frm.StartPosition = FormStartPosition.CenterScreen
        frm.ShowDialog()
        FillDataToGrid(False)
    End Sub

    Private Sub btnCopy_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnCopy.ItemClick
        Dim frm As New FrmCapNhatVatTu
        Dim strID = grvVatTu.GetRowCellValue(grvVatTu.FocusedRowHandle, grdColID).ToString
        Dim objEdit As VT_DM_VatTu = (From ds In dbTV.VT_DM_VatTus Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault

        frm.Action = vCopyAction
        frm.StartPosition = FormStartPosition.CenterScreen
        frm.VatTuEdit = objEdit
        frm.ShowDialog()
        FillDataToGrid(False)

    End Sub

    Private Sub btnXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXoa.ItemClick
        Try
            Dim strID As String = grvVatTu.GetRowCellValue(grvVatTu.FocusedRowHandle, grdColID).ToString
            Dim objCheckHDNhap = (From ds In dbTV.VT_HDNhap_ChiTiets Where ds.InUsed AndAlso ds.IDVatTu = strID Select ds).FirstOrDefault
            If objCheckHDNhap IsNot Nothing Then
                MsgErr("Tồn tại dữ liệu hóa đơn nhập")
                Return
            End If

            Dim objCheckHDXuat = (From ds In dbTV.VT_HDXuat_ChiTiets Where ds.InUsed AndAlso ds.IDVatTu = strID Select ds).FirstOrDefault
            If objCheckHDNhap IsNot Nothing Then
                MsgErr("Tồn tại dữ liệu hóa đơn xuất")
                Return
            End If

            Dim strMa As String = grvVatTu.GetRowCellValue(grvVatTu.FocusedRowHandle, grdColMaVatTu).ToString
            Dim strViTri As Integer = grvVatTu.FocusedRowHandle
            If MsgAskDelete(String.Format("Bạn có chắc chắn muốn xóa bản ghi {0} này không?", strMa)) = vMsg.vReSult.Yes Then
                Dim objXoa = (From ds In dbTV.VT_DM_VatTus Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
                If objXoa IsNot Nothing Then
                    objXoa.InUsed = False
                    objXoa.EditedOn = dbTV.GetDateTimeServer
                    iSubmitChange()
                    
                    FillDataToGrid()
                    Try
                        grvVatTu.FocusedRowHandle = strViTri
                    Catch ex As Exception

                    End Try
                End If
            End If
        Catch ex As Exception
            MsgInfo(ex.Message)
        End Try
    End Sub

    Private Sub btnSuaDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSuaDoi.ItemClick
        If grvVatTu.FocusedRowHandle < 0 Then
            MsgInfo("Bạn chưa chọn bản ghi để sửa")
            Return
        End If
        Dim strID = grvVatTu.GetRowCellValue(grvVatTu.FocusedRowHandle, grdColID).ToString
        Dim index As Integer = grvVatTu.FocusedRowHandle
        Dim objEdit As VT_DM_VatTu = (From ds In dbTV.VT_DM_VatTus Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
        If objEdit IsNot Nothing Then
            Dim frm As New FrmCapNhatVatTu
            frm.Action = vEditAction
            frm.VatTuEdit = objEdit
            frm.StartPosition = FormStartPosition.CenterScreen
            frm.ShowDialog()
            FillDataToGrid(False)
            grvVatTu.FocusedRowHandle = index
        Else
            MsgInfo("Không tồn tại dữ liệu")
        End If
    End Sub

    Private Sub btnNapLai_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnNapLai.ItemClick
        FillDataToGrid()
    End Sub

    Private Sub btnXuatExcel_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXuatExcel.ItemClick
        Export_Dialog(SaveFileDialog1, grdVatTu)
    End Sub

    Private Sub btnThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThoat.ItemClick
        LaThoat = True
        Me.Close()
    End Sub
End Class